import { NewsService } from './../../../service/blog/news.service';
import { ActivatedRoute } from '@angular/router';
import { Component, OnInit } from '@angular/core';
import {
  NewsHotData,
  NewsColumnData,
  NewestData,
  NominateData
} from 'src/app/model/NewsInfo';

@Component({
  selector: 'app-home',
  templateUrl: './home.page.html',
  styleUrls: ['./home.page.scss']
})
export class HomePage implements OnInit {
  type: number;
  url: string;
  newsHots: NewsHotData[] = [];
  newsColumns: NewsColumnData[];
  newestDatas: NewestData[];
  nominateDatas: NominateData[];
  constructor(
    private activatedRoute: ActivatedRoute,
    private newService: NewsService
  ) {
    this.type = this.activatedRoute.snapshot.data[0]['type'];
  }

  ngOnInit() {
    if (this.type == 1) {
      //栏目
      this.url = 'https://news-at.zhihu.com/api/3/sections';
    } else if (this.type == 2) {
      //热门
      this.url = 'https://news-at.zhihu.com/api/3/news/hot';
    } else if (this.type == 3) {
      //推荐
      this.url = 'https://news-at.zhihu.com/api/4/story/7101963/recommenders';
    } else if (this.type == 4) {
      //最新
      this.url = 'https://news-at.zhihu.com/api/4/news/latest';
    }
    this.getNews();
  }
  getNewsData() {
    this.newService.getNewsData();
  }
  getNews() {
    this.newService.getNews(this.url, res => {
      console.log('news home');
      console.log(JSON.stringify(res));
      if (this.type == 1) {
        this.newsColumns = res.data;
      } else if (this.type == 2) {
        this.newsHots = res.recent;
      } else if (this.type == 3) {
        this.nominateDatas = res.editors;
      } else if (this.type == 4) {
        // this.newestDatas = JSON.parse(JSON.stringify(res.stories));
        this.newestDatas=res.stories;
        console.log(this.newestDatas[0].images.length);
      }
    });
  }
}
